package com.shu.es;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpHost;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.PipelineAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;

public class EsDataMoveTest {

	
	public static void main(String[] args) throws Exception {
		String index = args[0];
		final String solrUrl = "http://127.0.0.1:8984/solr/"+index;
		RestHighLevelClient devClient = getDevClient();
		HttpSolrClient client = new HttpSolrClient.Builder(solrUrl)
			    .withConnectionTimeout(10000)
			    .withSocketTimeout(60000)
			    .build();


		SolrQuery query = new SolrQuery("*:*");  
		query.setRows(10000);
		query.setStart(0);//e2f6a5811f3745cb8f656e7c6ee5fadf  2330837573a247e988045fe32a06229d
        QueryResponse resp = client.query(query);  
        resp.getResults();
		

        int size = resp.getResults().size();
        SolrDocumentList list = resp.getResults();
        BulkRequest request = new BulkRequest(); 
        for(int i=0; i<size; i++) {
        	SolrDocument doc = list.get(i);
        	Collection<String> ks = doc.getFieldNames();
        	Map<String,Object> input = new HashMap<String,Object>();
        	for (String k : ks) {
        		if(!"_version_".equals(k)){
        			input.put(k, doc.getFieldValue(k));
        		}
        	}  
        	request.add(new IndexRequest(index+"1", "data",input.get("id").toString()).source(input));
        }
        request.timeout(TimeValue.timeValueMinutes(20));
        BulkResponse bulk = getDevClient().bulk(request);
        if(bulk.hasFailures()){
			bulk = getDevClient().bulk(request);
			if(bulk.hasFailures()){
				System.out.println("====================================ES data has submited fail! " );
				System.out.println("====================================ES data has submited fail! "+bulk.buildFailureMessage());
			}
		}else {
			System.out.println("====================================ES data has submited sucess! " +bulk.status());
		}
		client.close();
		devClient.close();
	}
	
	
	public static RestHighLevelClient getDevClient() {
		List<HttpHost> list = new ArrayList<HttpHost>();
		list.add(new HttpHost("192.168.1.16", 9200, "http"));
		list.add(new HttpHost("192.168.1.17", 9200, "http"));
		HttpHost[] arr = new HttpHost[list.size()];
		arr = list.toArray(arr);
		RestClientBuilder builder = RestClient.builder(arr);
		
		return new RestHighLevelClient(builder);
	}
	
	
}
